﻿@{
    Layout = null;
    ViewBag.Title = "组织机构管理——公路工程建设项目造价全过程管理系统";
}
@using KingRoad.EPM.Common.Helper
<script type="text/javascript">
    //树
    var zNodes_permission_organize_index = [];
    var zTree_permission_organize_index;
    var setting_permission_organize_index;
    var deleteID_permission_organize_index;
    var rootNodeId_permission_organize_index = 0; //记录根节点id
    var isStaff = false; //记录是否选择了职员
    //全局的href
    var href_permission_organize_index = "/Permission/organize";
    setting_permission_organize_index = {
        checkable: false,
        async: true,
        isSimpleData: true,
        editable: false,
        asyncUrl: "/Permission/Organize/GetOrganizeNodes/0",  //获取节点数据的URL地址
        //asyncDataFilter: ajaxDataFilter,
        nameCol: "Name",
        open: true,
        treeNodeKey: "Id",
        treeNodeParentKey: "ParentId",
        callback: {
            //             beforeAsync: zTreeBeforeAsync,
            asyncSuccess: zTreeOnAsyncSuccess_permission_organize_index,
            //             asyncError: zTreeOnAsyncError,
            click: zTreeOnClick_permission_organize_index
        }
    };

    function zTreeOnAsyncSuccess_permission_organize_index(event, treeId, treeNode, msg) {
        //异步成功
    }
    //树的单击事件
    function zTreeOnClick_permission_organize_index(event, treeId, treeNode) {
        isStaff = false; //点击一次树节点，该标记要初始化
        //递归获得根节点
        GetRootNodeObj(treeNode);
        deleteID_permission_organize_index = treeNode.Id; //点击树节点就记录可能删除的节点id
        var nodeId = treeNode.Id.substring(2, treeNode.Id.length);
        if (treeNode.Mark == "Company") {
            $("#addGS_permission_organize_index").show();
            $("#editGS_permission_organize_index").hide();
            $("#addDep_permission_organize_index").hide();
            $("#editDep_permission_organize_index").hide();
            $("#addDuty_permission_organize_index").show();
            //            $("#addStuff_permission_organize_index").hide();
            //            $("#editStuff_permission_organize_index").hide();
            //            $("#updatepic_permission_organize_index").hide();
            $("#del_permission_organize_index").show();
            $("#removeDuty_permission_organize_index").hide();
            //            $("#removeStaff_permission_organize_index").hide();
            //            $("#addCurrStaff_permission_organize_index").hide();
            $("#rightDetail_permission_organize_index").load("/Permission/organize/OrganizeDetails/" + nodeId, null, function () {
                //添加公司
                var addUrl_permission_organize_index = href_permission_organize_index + "/Create/" + nodeId;
                $("#addGS_permission_organize_index").attr("href", addUrl_permission_organize_index);
                //更新公司
                var editUrl_permission_organize_index = href_permission_organize_index + "/Edit/" + nodeId;
                $("#editGS_permission_organize_index").attr("href", editUrl_permission_organize_index);
                //添加部门
                var addDepUrl_permission_organize_index = href_permission_organize_index + "/CreateDep/" + nodeId;
                $("#addDep_permission_organize_index").attr("href", addDepUrl_permission_organize_index);
                //添加岗位
                var addDutyUrl_permission_organize_index = "/Permission/Duty/DutyTree/?id=" + treeNode.Id + "&name=" + escape(treeNode.Name);
                $("#addDuty_permission_organize_index").attr("href", addDutyUrl_permission_organize_index);
            });
        } else if (treeNode.Mark == "Department") {
            $("#addGS_permission_organize_index").hide();
            $("#editGS_permission_organize_index").hide();
            $("#addDep_permission_organize_index").show();
            $("#editDep_permission_organize_index").show();
            $("#addDuty_permission_organize_index").show();
            //            $("#addStuff_permission_organize_index").hide();
            //            $("#editStuff_permission_organize_index").hide();
            //            $("#updatepic_permission_organize_index").hide();
            $("#del_permission_organize_index").show();
            $("#removeDuty_permission_organize_index").hide();
            //            $("#removeStaff_permission_organize_index").hide();
            //            $("#addCurrStaff_permission_organize_index").hide();
            $("#rightDetail_permission_organize_index").load("/Permission/organize/OrganizeDetails/" + nodeId, null, function () {
                //添加部门
                var addDepUrl_permission_organize_index = href_permission_organize_index + "/CreateDep/" + nodeId;
                $("#addDep_permission_organize_index").attr("href", addDepUrl_permission_organize_index);
                //更新部门
                var editDepUrl_permission_organize_index = href_permission_organize_index + "/EditDep/" + nodeId;
                $("#editDep_permission_organize_index").attr("href", editDepUrl_permission_organize_index);
                //添加岗位
                var addDutyUrl_permission_organize_index = "/Permission/Duty/DutyTree/?id=" + treeNode.Id + "&name=" + escape(treeNode.Name);
                $("#addDuty_permission_organize_index").attr("href", addDutyUrl_permission_organize_index);
                //ajaxbg.hide();
            });
        } else if (treeNode.Mark == "Duty") {
            //对按钮做显示与隐藏
            $("#addGS_permission_organize_index").hide();
            $("#editGS_permission_organize_index").hide();
            $("#addDep_permission_organize_index").hide();
            $("#editDep_permission_organize_index").hide();
            $("#addDuty_permission_organize_index").hide();
            //            $("#addStuff_permission_organize_index").show();
            //            $("#editStuff_permission_organize_index").hide();
            //            $("#updatepic_permission_organize_index").hide();
            $("#del_permission_organize_index").hide();
            $("#removeDuty_permission_organize_index").show();
            //            $("#removeStaff_permission_organize_index").hide();
            //            $("#addCurrStaff_permission_organize_index").show();
            deleteID_permission_organize_index == null; //操作岗位是不显示删除按钮，也就不记录岗位id
            //*******2013.5.9 WS 改为采用用户而非职员********
            //添加用户，传递：公司ID、部门ID、岗位ID
            var pNode = zTree_permission_organize_index.getNodeByParam("Id", treeNode.ParentId);
            departId = pNode.Id;
            departName = pNode.Name;
            //根据部门与岗位ID，读取其内的所有用户
            $("#rightDetail_permission_organize_index").load("/Permission/User/Index?companyId=" + rootNodeId_permission_organize_index + "&departId=" + departId + "&dutyId=" + treeNode.Id.substring(0, treeNode.Id.lastIndexOf('_')) + "&departName=" + escape(departName) + "&dutyName=" + escape(treeNode.Name), null, function () {
                //                $("#addGS_permission_organize_index").attr("href", "/Permission/organize/Create/" + treeNode.Id);
                //                $("#editGS_permission_organize_index").attr("href", "/Permission/organize/Edit/" + treeNode.Id);
            });
            //**************************************************

            //            $("#addCurrStaff_permission_organize_index").attr("href", "/Permission/Organize/SelectOrganize?id=7_1&receiveId=" + treeNode.Id.substring(treeNode.Id.indexOf('_') + 1, treeNode.Id.length));
        }
    }
    function GetRootNodeObj(nodeObj) {
        if (nodeObj.ParentId == null || nodeObj.ParentId == 0) {
            rootNodeId_permission_organize_index = nodeObj.Id;
        }
        else {
            nodeObj = zTree_permission_organize_index.getNodeByParam("Id", nodeObj.ParentId);
            GetRootNodeObj(nodeObj);
        }
    }
    $(document).ready(function () {
        refreshTree("/Permission/Organize/GetOrganizeNodes/0");
        //对按钮做显示与隐藏
        $("#ul_permission_organize_index a").hide();
        $("#addGS_permission_organize_index").show();
        //模块删除操作
        $("#del_permission_organize_index").click(function () {
            //alert(zTree_permission_organize_index.getSelectedNode()==null);
            if (zTree_permission_organize_index.getSelectedNode()) {
                var sNode = zTree_permission_organize_index.getSelectedNode();
                if (deleteID_permission_organize_index == null || deleteID_permission_organize_index == 0) {
                    alertMsg.error("请先选择删除项！");
                    return false;
                }
                else {
                    alertMsg.confirm("您确定要删除吗？", {
                        okCall: function () {
                            if (isStaff)
                                $.post("/Permission/Organize/Delete/" + deleteID_permission_organize_index + "_" + sNode.Id, null, dialogdel_permission_organize_index, "json");
                            else
                                $.post("/Permission/Organize/Delete/" + deleteID_permission_organize_index, null, dialogdel_permission_organize_index, "json");
                        }
                    });
                }
            } else {
                alertMsg.error("请先选择要删除的项！");
                return false;
            }
        });
        //岗位移除
        $("#removeDuty_permission_organize_index").click(function () {
            if (zTree_permission_organize_index.getSelectedNode()) {
                var sNode1 = zTree_permission_organize_index.getSelectedNode();
                alertMsg.confirm("您确定要删除吗？", {
                    okCall: function () {
                        $.post("/Permission/Organize/RemoveConnectionOrgAndDuty/" + sNode1.Id, null, dialogdel_permission_organize_index, "json");
                    }
                });
            } else {
                alertMsg.error("请先选择要移除的项！");
                return false;
            }
        });

        $("#Permission_Organize_Index_SplitBar").click(function () {
            $("#Permission_Organize_Index_LeftPane").toggle();
            $("#Permission_Organize_Index_RightPane").toggleClass("Split_rightPanel");
        });
    });

    function refreshTree(asyncUrl) {
        $("#getUrl").html("");
        $("#onAsyncSuccessNode").html("<br/>");
        $("#onAsyncErrorNode").html("<br/>");
        setting_permission_organize_index.asyncUrl = asyncUrl;
        if ((typeof asyncUrl) == "function") {
            $("#asyncUrlCode").html("setAsyncUrl");
        } else {
            $("#asyncUrlCode").html("\"" + asyncUrl + "\"");
        }
        zTree_permission_organize_index = $("#treeorganize_permission_organize_index").zTree(setting_permission_organize_index, zNodes_permission_organize_index);

    }
    function dialogAdded_Permission_Organize(json) {
        //提交回调
        //1 接收参数 2 判断成功与否 3 根据返回的数据在树上新增节点
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //添加成功
            if (json.callbackType == "Create") {
                var newParentNode = zTree_permission_organize_index.getNodeByParam("Id", json.data.ParentId)
                //var selectedNode = zTree_permission_organize_index.getSelectedNode();
                zTree_permission_organize_index.addNodes(newParentNode, json.data);
            } else if (json.callbackType == "Edit") {
                //更新成功
                var selectedNode = zTree_permission_organize_index.getSelectedNode();
                if (selectedNode.Id == json.data.Id) {//更新当前选中的节点
                    selectedNode.Name = json.data.Name;
                    zTree_permission_organize_index.updateNode(selectedNode, true);
                } else {//将当前节点的父节点上移除，找到改变后的父节点并将当前新节点追加到该父节点上
                    zTree_permission_organize_index.removeNode(selectedNode, true);
                    var newParentNode = zTree_permission_organize_index.getNodeByParam("Id", json.data.ParentId);
                    zTree_permission_organize_index.addNodes(newParentNode, json.data);
                }
                var newNode = zTree_permission_organize_index.getNodeByParam("Id", json.data.Id);
                zTree_permission_organize_index.selectNode(newNode);
                if (newNode.Id.substring(0, 2) == "C_")
                    $("#rightDetail_permission_organize_index").load("/Permission/organize/OrganizeDetails/" + newNode.Id.substring(2, newNode.Id.length));
            }
            $.pdialog.closeCurrent();
        }
    }
    //模块删除回调函数
    function dialogdel_permission_organize_index(json) {
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //删除成功
            if (json.callbackType == "organize") {
                var selectedNode = zTree_permission_organize_index.getSelectedNode();
                zTree_permission_organize_index.removeNode(selectedNode, true);
                //删除后所有按钮的href需要初始化
                $("#addGS_permission_organize_index").attr("href", href_permission_organize_index + "/Create/0");
                $("#editGS_permission_organize_index").attr("href", href_permission_organize_index + "/Edit/{sid_user}");
                $("#addDep_permission_organize_index").attr("href", href_permission_organize_index + "/CreateDep/{sid_user}");
                $("#editDep_permission_organize_index").attr("href", href_permission_organize_index + "/EditDep/{sid_user}");
                $("#addDuty_permission_organize_index").attr("href", href_permission_organize_index + "/CreateDuty/{sid_user}");
                $("#editDuty_permission_organize_index").attr("href", href_permission_organize_index + "/EditDuty/{sid_user}");
                //                $("#addStuff_permission_organize_index").attr("href", href_permission_organize_index + "/CreateStaff/{sid_user}");
                //                $("#editStuff_permission_organize_index").attr("href", href_permission_organize_index + "/EditStaff/{sid_user}");
            } else {
                var delListID_organize_index = $("#List_Permission_Organize_DutyDetails").jqGrid("getGridParam", "selarrrow").toString();
                var delListID_arry = delListID_organize_index.split(',');
                //                for (var i = 0; i < delListID_arry.length; i++) {
                //                    $("#List_Permission_Organize_DutyDetails").delRowData(delListID_arry[i]);
                //                }
                //更新JqGrid
                $("#List_Permission_Organize_DutyDetails").trigger("reloadGrid");
                alertMsg.correct("成功删除" + delListID_arry.length.toString() + "条记录！");

            }
            deleteID_permission_organize_index = null;
        }
    }
    function dialogCreated_Permission_Organize_CreateUser(json) {
        //提交回调
        //1 接收参数 2 判断成功与否 3 根据返回的数据在树上新增节点
        DWZ.ajaxDone(json);
        //alert(json.callbackType);
        if (json.statusCode == DWZ.statusCode.ok) {
            //添加成功
            var user_array = new Array();
            user_array = json.callbackType.split(',');
            $("#UserId").val(user_array[0]);
            $("#UserName").val(user_array[1]);
            $.pdialog.closeCurrent();
        }
    }

    
</script>
<div class="page">
    @*顶部按钮*@
    <div class="panelBar">
        <ul class="toolBar" id="ul_permission_organize_index">
            <li>@Html.PermissionButton("Permission.Organize.Create", "addGS_permission_organize_index", "add", "/Permission/organize/Create/0", "new_addGS", "dialog", "", "true", "410", "460", "添加部门")</li>
            <li>@Html.PermissionButton("Permission.Organize.Edit", "editGS_permission_organize_index", "edit", "/Permission/organize/Edit/{sid_user}", "new_editGS", "dialog", "", "true", "410", "460", "编辑部门")</li>
            <li>@Html.PermissionButton("Permission.Organize.CreateDep", "addDep_permission_organize_index", "add", "/Permission/organize/CreateDep/{sid_user}", "new_addDep", "dialog", "", "true", "410", "430", "添加部门")</li>
            <li>@Html.PermissionButton("Permission.Organize.EditDep", "editDep_permission_organize_index", "edit", "/Permission/organize/EditDep/{sid_user}", "new_editDep", "dialog", "", "true", "410", "430", "编辑部门")</li>
            <li>@Html.PermissionButton("Permission.Organize.CreateDuty", "addDuty_permission_organize_index", "add", "/Permission/Duty/DutyTree/{sid_user}", "new_addDuty", "dialog", "", "true", "280", "350", "添加岗位")</li>
            <li>@Html.PermissionButton("Permission.Organize.RemoveDuty", "removeDuty_permission_organize_index", "delete", "javascript:void(0);", "", "", "", "", "移除岗位")</li>
            @*<li>@Html.PermissionButton("Permission.Organize.CreateStaff", "addStuff_permission_organize_index", "add", "/Permission/organize/CreateStaff/{sid_user}", "new_addStaff", "dialog", "", "true", "410", "490", "添加职员")</li>
            <li>@Html.PermissionButton("Permission.Organize.AddCurrStaff", "addCurrStaff_permission_organize_index", "add", "", "new_addDuty", "dialog", "", "true", "280", "350", "添加已有职员")</li>
            <li>@Html.PermissionButton("Permission.Organize.EditStaff", "editStuff_permission_organize_index", "edit", "/Permission/organize/EditStaff/{sid_user}", "new_editStaff", "dialog", "", "true", "410", "475", "编辑职员")</li>
            <li>@Html.PermissionButton("Permission.Organize.EditStaff", "updatepic_permission_organize_index", "edit", "/Permission/organize/UpdatePic/{sid_user}", "new_editStaff", "dialog", "", "true", "450", "360", "上传签名")</li>
            <li>@Html.PermissionButton("Permission.Organize.RemoveStaff", "removeStaff_permission_organize_index", "delete", "/PermissionOrganize/RemoveConnectionOrgAndDuty", "", "", "", "", "移除职员")</li>*@
            <li class="line">line</li>
            <li>@Html.PermissionButton("Permission.Organize.Delete", "del_permission_organize_index", "delete", "javascript:void(0);", "", "", "", "", "删除")</li>
        </ul>
    </div>
    <div id="mainContent_permission_organize_index" layouth="27">
        <div id="Permission_Organize_Index_LeftPane" class="leftPanel" style="width:300px;">
            @*菜单树*@
            <ul id="treeorganize_permission_organize_index" class="ztree">
            </ul>
        </div>
        <div id="Permission_Organize_Index_RightPane" class="rightPanel" style="margin-left:301px;">
            @*表单内容*@
            <div id="rightDetail_permission_organize_index">
            </div>
        </div>
    </div>
</div>
